package com.warrior.superdata.controller;

import com.warrior.superdata.annotation.MysqlCheckInject;
import com.warrior.superdata.base.CommonResponse;
import com.warrior.superdata.base.CommonResult;
import com.warrior.superdata.constants.CommonConstant;
import com.warrior.superdata.mysql.entity.ApiDatasource;
import com.warrior.superdata.mysql.entity.dto.ApiDatasourceDto;
import com.warrior.superdata.mysql.entity.dto.TableFieldDto;
import com.warrior.superdata.mysql.service.ApiDatasourceSerivce;
import com.warrior.superdata.exceptionhandler.ApplicationException;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @ClassName: ApiDatasourceController.java
 * @date: 2022/3/2 18:05
 * @author xujian
 */
@ApiSort(value = 2)
@Api(tags = "数据源接口")
@RestController
@RequestMapping("/api/datasource/")
public class ApiDatasourceController {

    @Autowired
    private ApiDatasourceSerivce apiDatasourceSerivce;

    @ApiOperation(position = 1, value = "数据源测试链接")
    @PostMapping("/connect")
    @ApiImplicitParam(name = "apiDatasourceDto", value = "数据源实体", dataType = "ApiDatasourceDto")
    public CommonResult connet(@RequestBody @Validated ApiDatasourceDto apiDatasourceDto) throws ApplicationException {
        return CommonResponse.success(apiDatasourceSerivce.testSourceConnect(apiDatasourceDto));
    }

    @ApiOperation(position = 2, value = "新增数据源接口")
    @PostMapping("")
    @ApiImplicitParam(name = "apiDatasourceDto", value = "数据源实体", dataType = "ApiDatasourceDto")
    @MysqlCheckInject
    public CommonResult addDatasource(@RequestBody ApiDatasourceDto apiDatasourceDto) {
        apiDatasourceSerivce.addDatasource(apiDatasourceDto);
        return CommonResponse.success(CommonConstant.CONSTANT_INSERT_SUCCESS);
    }

    @ApiOperation(position = 3, value = "数据源列表接口")
    @GetMapping("")
    @ApiImplicitParam(name = "type", value = "数据类型", dataType = "int", paramType = "query")
    public CommonResult<ApiDatasource> addDatasource(Integer type) {
        return CommonResponse.success(apiDatasourceSerivce.getDatasourceList(type));
    }

    @ApiOperation(position = 4, value = "获取数据源下的所有的表")
    @GetMapping("/tables")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "datasourceCode", value = "数据源code", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "dataBaseName", value = "数据库名称", dataType = "String", paramType = "query")}
    )
    public CommonResult<ApiDatasource> getTables(String datasourceCode, String dataBaseName) {
        return CommonResponse.success(apiDatasourceSerivce.getTables(datasourceCode, dataBaseName));
    }

    @ApiOperation(position = 5, value = "获取表下所有的字段")
    @GetMapping("/table/field")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "datasourceCode", value = "数据源code", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "dataBaseName", value = "数据库名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "tableName", value = "表名", dataType = "String", paramType = "query")
    })
    public CommonResult<List<TableFieldDto>> getTableFields(String datasourceCode, String dataBaseName, String tableName) {
        return CommonResponse.success(apiDatasourceSerivce.getTableFields(datasourceCode, dataBaseName, tableName));
    }

    @ApiOperation(position = 6, value = "获取数据源下的所有的数据库")
    @GetMapping("/database")
    @ApiImplicitParam(name = "datasourceCode", value = "数据源code", dataType = "String", paramType = "query")
    public CommonResult<ApiDatasource> getDatabases(String datasourceCode) {
        return CommonResponse.success(apiDatasourceSerivce.getDatabases(datasourceCode));
    }

}
